<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包</title>
</head>
<body>
    <script>
         //闭包写法1，
        // function fn () {        //定义函数
        //    var a = 1 
        //     function fn1 () {       //fn函数中的fn1函数
        //         console.log(a + 1)
        //     }
        //     fn1 ()
        // }
        // fn()

        //闭包写法2（回调函数写法）
        // function fn (cb) {
        //     var a = 1
        //     cb(a)
        // }
        // function callback (a) {
        //     console.log(a + 1)
        // }
        // fn(callback)

        // function books () {
        //     var book = '书包里的书本'
        //     return function () {
        //         console.log(book)
        //     }
        // }
        // var bag = books()
        // bag()

        //闭包
        function fn (){
            var arr = []
            for (var i = 0; i< 10; i++){
                arr[i] = function fn (){
                    return i 
                }
            }
            return arr
        }
        var arr = fn()
        console.log(arr[0]())
    </script>
</body>
</html>